package com.weishi.project.api.controller;

import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.project.api.ret.NaturalYearRet;
import com.weishi.project.api.vo.DictVo;
import com.weishi.project.training.domain.AnnualPlan;
import com.weishi.project.training.domain.AnnualProject;
import com.weishi.project.training.domain.NaturalYear;
import com.weishi.project.training.domain.TrainingGroupChannel;
import com.weishi.project.training.service.IAnnualPlanService;
import com.weishi.project.training.service.IAnnualProjectService;
import com.weishi.project.training.service.INaturalYearService;
import com.weishi.project.training.service.ITrainingWeekService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/annualPlan")
@Api(value = "AnnualPlan", description = "年度目标及计划制定")
public class ApiAnnualPlanController extends BaseController {

    @Autowired
    private INaturalYearService naturalYearService;

    @Autowired
    private IAnnualPlanService annualPlanService;

    @Autowired
    private IAnnualProjectService annualProjectService;

    @Autowired
    private ITrainingWeekService trainingWeekService;

    @ApiOperation(value = "获取年度数据")
    @GetMapping("/naturalYearList")
    public AjaxResult naturalYearList() {
        List<NaturalYear> list = naturalYearService.selectNaturalYearList(new NaturalYear());
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "获取自然年份集合")
    @GetMapping("/getNaturalYearList")
    public AjaxResult getNaturalYearList() {
        List<NaturalYear> list = naturalYearService.getNaturalYearList();
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "更新自然年新添的三个字段")
    @PostMapping("/updateNaturalYear")
    public AjaxResult updateNaturalYear(@RequestBody NaturalYear naturalYear) {
        return AjaxResult.success(naturalYearService.updateById(naturalYear));
    }


    @ApiOperation(value = "获取自然年新添的三个字段")
    @GetMapping("/getNaturalYearDetails")
    public AjaxResult getNaturalYearDetails(@RequestParam int id) {
        return AjaxResult.success(naturalYearService.getById(id));
    }


    @ApiOperation(value = "获取年度下拉数据")
    @GetMapping("/naturalYearTree")
    public AjaxResult naturalYearTree() {
        List<NaturalYearRet> list = naturalYearService.selectNaturalYearRetList(true);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "查询年度目标列表")
    @GetMapping("/list")
    public AjaxResult list(AnnualPlan annualPlan) {
        List<AnnualPlan> list = annualPlanService.selectAnnualPlanList(annualPlan);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "获取年度目标详细信息")
    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(annualPlanService.getAnnualPlanById(id));
    }

    @ApiOperation(value = "新增年度目标")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Map<String, List<AnnualPlan>> params) {
        List<AnnualPlan> annualPlans = params.get("annualPlans");
        for (AnnualPlan annualPlan : annualPlans) {
            annualPlan.setCtime(new Date());
            annualPlanService.save(annualPlan);
            trainingWeekService.saveTrainingWeek(annualPlan,annualPlan.getId());
        }
        return AjaxResult.success();
    }

    @ApiOperation(value = "修改年度目标")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Map<String, List<AnnualPlan>> params) throws ParseException {
        List<AnnualPlan> annualPlans = params.get("annualPlans");
        for (AnnualPlan annualPlan : annualPlans) {
            annualPlan.setLtime(new Date());
            if (annualPlan.getId() != null) {
                AnnualPlan annualPlan1 = annualPlanService.getAnnualPlanById(annualPlan.getId());
                if (!(annualPlan1.getStartTime().equals(annualPlan.getStartTime())) || !(annualPlan1.getEndTime().equals(annualPlan.getEndTime()))) {
                    trainingWeekService.removeTrainingWeek(annualPlan.getId());
                    trainingWeekService.saveTrainingWeek(annualPlan,annualPlan.getId());
                }
                annualPlanService.updateAnnualPlanById(annualPlan);
            } else {
                annualPlanService.save(annualPlan);
                trainingWeekService.saveTrainingWeek(annualPlan,annualPlan.getId());
            }
        }
        return AjaxResult.success();
    }

    @ApiOperation(value = "删除年度目标")
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        annualPlanService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    @ApiOperation(value = "获取年度计划训练项目信息")
    @GetMapping("/annualProject")
    public AjaxResult gInAnnualProjectInfo(@RequestParam Integer naturalYearId) {
        AnnualProject annualProject = new AnnualProject();
        annualProject.setNaturalYearId(naturalYearId);
//        annualProject.setId(1);
//        annualProject.setClassHour(4);
//        annualProject.setManKilometre(10.5);
//        annualProject.setWomenKilometre(5.5);
//        annualProject.setManTime((long)100);
//        annualProject.setWomenTime((long)150);
//        annualProject.setProjectName("水上有氧划行");
        List<AnnualProject> list = annualProjectService.selectAnnualProjectList(annualProject);
//        list.add(annualProject);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "修改年度计划训练项目信息")
    @PostMapping("/annualProject/edit")
    public AjaxResult annualProjectEdit(@RequestBody Map<String, List<AnnualProject>> params) {
        List<AnnualProject> annualProjects = params.get("annualProjects");
        for (AnnualProject annualProject : annualProjects) {
            if(annualProject.getId()!=null){
                annualProject.setLtime(new Date());
                annualProjectService.updateByIdTo(annualProject);
            }else{
                annualProject.setLtime(new Date());
                annualProject.setCtime(new Date());
                annualProjectService.save(annualProject);
            }
        }
        return AjaxResult.success();
    }


    @ApiOperation(value = "一级二级项目级联")
    @GetMapping("/totalLevelList")
    public AjaxResult dictList() {

        List<DictVo> dictOneList = annualPlanService.getDictVoOneList();
        for (DictVo dictVo : dictOneList) {
            dictVo.setTwoList(annualPlanService.getDictVoTwoList(dictVo.getDictValue()));
        }
        return AjaxResult.success(dictOneList);
    }

    @GetMapping("/show")
    public AjaxResult updateAchievement() throws ParseException {
        List<TrainingGroupChannel> channels = annualPlanService.getAchievementList();
        for (TrainingGroupChannel trainingGroupChannel : channels) {

//            SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
//                Date datenum = sf.parse(trainingGroupChannel.getAchievement());
//                Calendar calendar = Calendar.getInstance();
//                calendar.setTime(datenum);
//                calendar.getTimeInMillis();
//                System.out.println(calendar.getTimeInMillis());

        }
        return AjaxResult.success();
    }


}
